package com.baobao.web.rest;

import com.baobao.dao.NoticeDAO;

import com.baobao.domain.Notice;

import com.baobao.service.NoticeService;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Notice entities
 * 
 */

@Controller("NoticeRestController")
public class NoticeRestController {

	/**
	 * DAO injected by Spring that manages Notice entities
	 * 
	 */
	@Autowired
	private NoticeDAO noticeDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Notice entities
	 * 
	 */
	@Autowired
	private NoticeService noticeService;

	/**
	 * Select an existing Notice entity
	 * 
	 */
	@RequestMapping(value = "/Notice/{notice_schoolId}/{notice_time}", method = RequestMethod.GET)
	@ResponseBody
	public Notice loadNotice(@PathVariable String notice_schoolId, @PathVariable Calendar notice_time) {
		return noticeDAO.findNoticeByPrimaryKey(notice_schoolId, notice_time);
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Save an existing Notice entity
	 * 
	 */
	@RequestMapping(value = "/Notice", method = RequestMethod.PUT)
	@ResponseBody
	public Notice saveNotice(@RequestBody Notice notice) {
		noticeService.saveNotice(notice);
		return noticeDAO.findNoticeByPrimaryKey(notice.getSchoolId(), notice.getTime());
	}

	/**
	 * Delete an existing Notice entity
	 * 
	 */
	@RequestMapping(value = "/Notice/{notice_schoolId}/{notice_time}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteNotice(@PathVariable String notice_schoolId, @PathVariable Calendar notice_time) {
		Notice notice = noticeDAO.findNoticeByPrimaryKey(notice_schoolId, notice_time);
		noticeService.deleteNotice(notice);
	}

	/**
	 * Create a new Notice entity
	 * 
	 */
	@RequestMapping(value = "/Notice", method = RequestMethod.POST)
	@ResponseBody
	public Notice newNotice(@RequestBody Notice notice) {
		noticeService.saveNotice(notice);
		return noticeDAO.findNoticeByPrimaryKey(notice.getSchoolId(), notice.getTime());
	}

	/**
	 * Show all Notice entities
	 * 
	 */
	@RequestMapping(value = "/Notice", method = RequestMethod.GET)
	@ResponseBody
	public List<Notice> listNotices() {
		return new java.util.ArrayList<Notice>(noticeService.loadNotices());
	}
}